home *** CD-ROM | disk | FTP | other *** search
- /*
- * $RCSfile: generic.c,v $
- * $Revision: 1.1.1.1 $
- * $Date: 1996/05/04 21:55:34 $
- */
- /**********************************************************************
- * EXODUS Database Toolkit Software
- * Copyright (c) 1991 Computer Sciences Department, University of
- * Wisconsin -- Madison
- * All Rights Reserved.
- *
- * Permission to use, copy, modify and distribute this software and its
- * documentation is hereby granted, provided that both the copyright
- * notice and this permission notice appear in all copies of the
- * software, derivative works or modified versions, and any portions
- * thereof, and that both notices appear in supporting documentation.
- *
- * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
- * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.
- * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
- * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
- *
- * The EXODUS Project Group requests users of this software to return
- * any improvements or extensions that they make to:
- *
- * EXODUS Project Group
- * c/o David J. DeWitt and Michael J. Carey
- * Computer Sciences Department
- * University of Wisconsin -- Madison
- * Madison, WI 53706
- *
- * or exodus@cs.wisc.edu
- *
- * In addition, the EXODUS Project Group requests that users grant the
- * Computer Sciences Department rights to redistribute these changes.
- **********************************************************************/
-
- #include <stdio.h>
- #include "ess.h"
- #include "bit_funcs.h"
-
-
- /* Module bit.c : routines for bit maps manipulation
-
-
- IMPORTS: NONE
-
- EXPORTS: Bit map manipulation routines
-
- clearmap(mapptr, mapsize) : clear all the bits in the map
-
- setmap(mapptr, mapsize) : set all the bits in the map
-
- countmap(mapptr, mapsize) : count and return the number of bit set
-
- mapempty(mapptr, mapsize) : check if all the bits in the bit map
- are cleared, return TRUE if so, FALSE otherwise
-
- setbit(mapptr, pos) : set the (pos)th bit in the bit map
-
- clearbit(mapptr, pos) : clear the (pos)th bit in the bit map
-
- checkset(mapptr, pos) : check if the (pos)th bit in the bit map is set,
- return TRUE if so, FLASE otherwise
-
- nextset(mapptr, mapsize, last) : given the position of the last bit
- found, find the next bit set and return its position
-
- nextclear(mapptr, mapsize, last) : given the position of the last bit
- found, find the next bit cleared and return its position
-
- shiftmap(mapptr, pos, len, amt) shift bits (pos) to (pos+len-1)
- of the bitmap amt bits. If amt > 0, bits are shifted right.
- If amt < 0, bits are shifted left.
-
- Actually, this is a really lousy description of what
- shiftmap does. After testing the assembly language version,
- what shiftmap actually does is replicate the pattern of bits
- from positions (pos) to (pos+len-1) at the positions
- (pos+amt) to (pos+len+amt-1)
-
-
- NOTES: Bits are numbered from 0 through (mapsize - 1).
- */
-
-
- /* these routines are derived from wiss's bit.c. the shiftmap routine
- was added by dewitt 11/1/88 as, while shiftmap appeared in bit.s
- it did not appear in bit.c */
-
- #define tBITMAP 0
-
- void
- printmap (
- unsigned char *mapptr, /* start of bitmap */
- int mapsize /* size of bitmap (bits) */
- )
- /* print a given bit map (on the standard output) in hexdecimal form
-
- Returns:
- NONE
-
- Side Effects:
- bit map printed on the standard output decive in hexdecimal
-
- Errors:
- NONE
- */
- {
- register int i;
- int bytes;
-
- bytes = (mapsize-1)/BITSPERBYTE + 1;
- printf("{");
- for (i = 0; i < bytes; i++)
- printf(i==0 ? "%03x" : " %03x", mapptr[i]);
- printf("}");
- }
-
-
- /*
- **************************************************************
- * map-wise operations *
- **************************************************************
- */
-
-
- void
- clearmap(
- unsigned char *mapptr, /* start of bitmap */
- int mapsize /* size of bitmap (bits) */
- )
- /* clear all the bits in the given bit map
-
- Returns:
- NONE
-
- Side Effects:
- All the bits in the bit map cleared
-
- Errors:
- NONE
- */
- {
- register int count;
-
- for (count = (--mapsize / BITSPERBYTE) + 1; count--; mapptr++)
- *mapptr = 0; /* clear word */
- }
-
- void
- setmap(
- unsigned char *mapptr, /* start of bitmap */
- int mapsize /* size of bitmap (bits) */
- )
- /* set all the bits in the given map
-
- Returns:
- NONE
-
- Side Effects:
- All the bits in the bit map cleared
-
- Errors:
- NONE
- */
- {
- register int count;
-
- for (count = (--mapsize / BITSPERBYTE) + 1; count--; mapptr++)
- *mapptr = ~0; /* set word */
- }
-
- int
- countmap(
- unsigned char *mapptr, /* start of bitmap */
- int mapsize /* size of bitmap (bits) */
- )
- /* count and return the number of bit set in the bit map
-
- Returns:
- Number of bit set in the bit map
-
- Side Effects:
- NONE
-
- Errors:
- NONE
- */
- {
- register int count, mask;
-
- for (count = 0, mask = 1; mapsize; mapsize--)
- {
- if (*mapptr & mask) /* bit set */
- count++;
-
- if ((mask<<=1) == (1<<BITSPERBYTE)) /* advance to next bit */
- { /* cross word boundary */
- mask = 1; /* reset mask to 1 */
- mapptr++; /* advance map pointer */
- }
- }
-
- return(count);
- }
-
- mapempty(
- unsigned char *mapptr, /* start of bitmap */
- int mapsize /* size of bitmap (bits) */
- )
- /* check if all the bits in the given bit map are cleared
-
- Returns:
- TRUE if all the bits are cleared,
- FALSE otherwise
-
- Side Effects:
- NONE
-
- Errors:
- NONE
- */
- {
- register int mask;
-
- for (mask = 1; mapsize; mapsize--)
- {
- if (*mapptr & mask) /* bit set */
- return(FALSE);
-
- if ((mask<<=1) == (1<<BITSPERBYTE)) /* advance to next bit */
- { /* cross word boundary */
- mask = 1; /* reset mask to 1 */
- mapptr++; /* advance map pointer */
- }
- }
-
- return(TRUE);
- }
-
-
- /*
- **************************************************************
- * bit-wise operations *
- **************************************************************
- */
-
-
- void
- setbit(
- unsigned char *mapptr,
- int pos /* bit position */
- )
- /* set the (pos)th bit in the bit map
-
- Returns:
- NONE
-
- Side Effects:
- the (pos)th bit in the bit map set
-
- Errors:
- NONE
- */
- {
- /* adjust word pointer and bit offset (pos) */
- mapptr += pos / BITSPERBYTE;
- pos %= BITSPERBYTE;
-
- *mapptr |= 1 << pos; /* set bit */
- }
-
- void
- clearbit(
- unsigned char *mapptr,
- int pos /* bit position */
- )
- /* clear the (pos)th bit in the bit map
-
- Returns:
- NONE
-
- Side Effects:
- the (pos)th bit in the map cleared
-
- Errors:
- NONE
- */
- {
- /* adjust word pointer and bit offset (pos) */
- mapptr += pos / BITSPERBYTE;
- pos %= BITSPERBYTE;
-
- *mapptr &= ~(1 << pos); /* clear bit */
- }
-
- checkset(
- unsigned char *mapptr,
- int pos /* bit position */
- )
- /* check if the (pos)th bit in the bit map is set
-
- Returns:
- TRUE if the (pos)th bit in the map is set
- FLASE otherwise
-
- Side Effects:
- NONE
-
- Errors:
- NONE
- */
- {
- /* adjust word pointer and bit offset (pos) */
- mapptr += pos / BITSPERBYTE;
- pos %= BITSPERBYTE;
-
- return((*mapptr & (1 << pos)) ? TRUE : FALSE);
- }
-
- nextset(
- unsigned char *mapptr,
- int mapsize, /* start and size of the bit map */
- int last /* last bit position */
- )
- /* given the position of the last bit found, find the next bit set in the map
-
- Returns:
- the position of the next set bit in the map
- -1 if no more, or if bad "last found" value passed in
-
- Side Effects:
- NONE
-
- Errors:
- NONE
- */
- {
- register int mask;
-
- if (last < -1)
- last = -1;
- else if (last >= mapsize)
- return(-1);
-
- /* set up starting bit position */
- mapptr += (++last) / BITSPERBYTE;
- mask = 1 << (last % BITSPERBYTE);
-
- for (mapsize -= last; /* mapsize = # checkable bits left */
- mapsize; /* any bits left to check? */
- last++, mapsize--) /* inc position, dec counter */
- {
- if (*mapptr & mask) /* next bit found */
- return (last);
-
- if ((mask<<=1) == (1<<BITSPERBYTE)) /* advance to next bit */
- { /* cross word boundary */
- mask = 1; /* reset mask to 1 */
- mapptr++; /* advance map pointer */
- }
- }
-
- return(-1);
- }
-
-
- nextclear(
- unsigned char *mapptr,
- int mapsize, /* start and size of the bit map */
- int last /* last bit position */
- )
- /* given position of the last bit found, find the next bit cleared in the map
-
- Returns:
- the position of the next cleared bit in the map
-
- Side Effects:
- NONE
-
- Errors:
- NONE
- */
- {
- register int mask;
-
- if (last < -1 || last >= mapsize)
- return(-1);
-
- /* set up starting bit position */
- mapptr += (++last) / BITSPERBYTE;
- mask = 1 << (last % BITSPERBYTE);
-
- for (mapsize -= last; /* mapsize = # checkable bits left */
- mapsize; /* any bits left to check? */
- last++, mapsize--) /* inc position, dec counter */
- {
- if ((*mapptr & mask) == 0) /* next bit found */
- return (last);
-
- if ((mask<<=1) == (1<<BITSPERBYTE)) /* advance to next bit */
- { /* cross word boundary */
- mask = 1; /* reset mask to 1 */
- mapptr++; /* advance map pointer */
- }
- }
-
- return(-1);
- }
-
-
- /*
-
- shift bits (pos) to (pos+len-1) of the bitmap amt bits.
- If amt > 0, bits are shifted right. If amt < 0, bits are shifted left.
-
- Actually, this is a really lousy description of what shiftmap does.
- After testing the assembly language version, what shiftmap actually
- does is replicate the pattern of bits from positions (pos) to (pos+len-1)
- at the positions (pos+amt) to (pos+len+amt-1)
-
- */
-
- void
- shiftmap(
- unsigned char *mapptr,
- int pos, /* starting bit position */
- int len, /* length of region to be shifted */
- int amt /* number of bits the region is to be shifted */
- /* if amt < 0, bits are shifted left. if amt > 0
- bits are shifted right */
- )
- {
- register int curbit;
- register int i;
-
- if (amt > 0)
- {
- /* shift right */
- curbit = pos + len - 1; /* point at the last bit in the region as we
- are going to start at the right and work left */
- for (i=0;i<len;i++, curbit--)
- {
- if (checkset(mapptr, curbit))
- {
- setbit(mapptr, curbit+amt);
- }
- else clearbit(mapptr, curbit+amt);
- }
- }
- else /* amt < 0 - shift left */
- {
- curbit = pos; /* start at first bit in region and move right */
- for (i=0;i<len;i++, curbit++)
- {
- if (checkset(mapptr, curbit))
- {
- setbit(mapptr, curbit+amt);
- }
- else clearbit(mapptr, curbit+amt);
- }
- }
- }
-